import java.util.Vector;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;


public class BtClient implements DiscoveryListener {

	Vector discoveredDevices = new Vector();
	
	public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass) {
		
	}

	public void inquiryCompleted(int arg0) {
		
	}

	public void servicesDiscovered(int transID, ServiceRecord[] serviceRecord) {
		// TODO Auto-generated method stub
		
	}

	public void serviceSearchCompleted(int transID, int responseCode) {
		// TODO Auto-generated method stub
		
	}
	
	public void btInitiateDeviceSearch() {
		System.out.println("BTMIDlet.btInitiateDeviceSeach");
		int i, s;
		
		remoteDevices.clear();
		discoveredDevices.removeAllElements();
		
		RemoteDevice[] cachedDevices = discoveryAgent.retriveDevices(DiscoveryAgent.CACHED);
		if (cachedDevices != null) {
			s = cachedDevices.length;
			for (i=0; i<s; i++) {
				remoteDevices.put(cachedDevices[i].getBluetoothAddress(), cachedDevices[i]);
			}
		}
		
		RemoteDevice[] preknownDevices = discoveryAgent.retriveDevices(DiscoveryAgent.PREKNOWN);
		if (preknownDevices != null) {
			s = cachedDevices.length;
			for (i=0, i<s, i++) {
				remoteDevices.put(cachedDevices[i].getBluetoothAddress(), cachedDevices[i]);
			}
		}

		try {
			inquiryStarted = discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
		} catch (BluetoothStateException bse) {
			System.out.println("Inquiry Failed");
			return;
		}
		
		if (inquiryStarted == true) {
			System.out.println("Inquiry in Progress");
		} else {
			System.out.println("Inquiry Failed");
		}
		
	}
}
